import React, {Component, PropTypes} from 'react';
import styles from './Main.less';
import Signals from 'signals';
import ShareTableComponent from '../Table/ShareTableComponent';
import ShareTreeContainer from '../Tree/ShareTreeContainer';
import {getTableInfo} from "../../services/showServer";
import {getSubjectContentTree} from "../../services/showServer";

function GetQueryString(name)
{
  var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if(r!=null)return  unescape(r[2]); return null;
}

class ShareMainContainer extends React.Component {
  constructor(props) {
    super(props);
    this.treeSignal = new Signals.Signal();
    this.state = {};
  }

  componentDidMount() {
    var id = GetQueryString("id");
    getSubjectContentTree(id).then(function (json) {
      this.refs.tree.setState({
        id: json.id,
        subname: json.name,
        data: json
      });
    }.bind(this));
  }

  componentWillMount() {
    this.treeSignal.add(function (info) {
      getTableInfo(info).then(function (json) {
        var meta = JSON.parse(json.tableMeta);
        var pColumns = [];
        var columns = [];
        if (meta.partitioned == 'true') {
          pColumns = meta.partitionColumns;
        }
        if (meta.columns != '') {
          columns = meta.columns;
        }

        this.refs.table.setState({
          remark: json.remark,
          editComment: json.comment,
          comment: json.comment,
          partitionColumns: pColumns,
          columnsFilter: columns,
          columns: columns,
          tableMeta: meta,
          table: json,
        });

      }.bind(this));
    }.bind(this));

  }

  componentWillUnmount() {
    this.treeSignal.removeAll();
  }

  render() {
    return (
      <div className={styles.content}>
        <div className={styles.share_lside}>
          <ShareTreeContainer signal={this.treeSignal} ref="tree"/>
        </div>
        <div className={styles.share_main}>
          <ShareTableComponent ref="table"/>
        </div>
      </div>
    );
  }
}

export default ShareMainContainer;
